package com.ruoyi.erecord.iot.tools;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.security.SecureRandom;
import java.util.Random;
import java.util.UUID;

/**
 * 封装各种生成唯一性ID算法的工具类.
 *
 * @author Fullteem
 * @version 2013-01-15
 */
@Service
@Lazy(false)
public class IDGen/* implements SessionIdGenerator*/ {

    static SecureRandom random = new SecureRandom();

    static IDWorker worker = new IDWorker(0, 0);

    /**
     * 封装JDK自带的UUID, 通过Random数字生成, 中间无-分割.
     */
    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 使用SecureRandom随机生成Long.
     */
    public static long randomLong() {
        return Math.abs(random.nextLong());
    }


    /**
     * Activiti ID 生成
     */

    public String getNextId() {
        return IDGen.uuid();
    }

    //    public Serializable generateId(Session session) {
    //        return IdGen.uuid();
    //    }

    public static void main(String[] args) {
        //		System.out.println(IdGen.uuid());
        //		System.out.println(IdGen.uuid().length());
        //		System.out.println(new IdGen().getNextId());
        //		for (int i=0; i<1000; i++){
        //			System.out.println(IdGen.randomLong() + "  " + IdGen.randomBase62(5));
        //			System.out.println(IdGen.timeId());
        //		}
        System.out.println(IDGen.longTimeId());
    }

    private static long currentTime;
    private static long currentNum = 0;

    public static String appId() {
        int first = new Random(10).nextInt(8) + 1;
        System.out.println(first);
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if (hashCodeV < 0) {//有可能是负数
            hashCodeV = -hashCodeV;
        }
        // 0 代表前面补充0
        // 4 代表长度为4
        // d 代表参数为正数型
        return first + String.format("%015d", hashCodeV);
    }

    public static String strTimeId() {
        long time = System.currentTimeMillis();
        if (currentTime != time) {
            currentTime = time;
            currentNum = 0;
        } else {
            currentNum += 1;
        }
        return time + String.valueOf(currentNum) + random.nextInt(1000);
    }

    public static long longTimeId() {
        return Long.parseLong(strTimeId());
    }


    public static String stockOutBatch() {
        long time = System.currentTimeMillis() / 1000;
        if (currentTime != time) {
            currentTime = time;
            currentNum = 0;
        } else {
            currentNum += 1;
        }
        return "8002-" + time + currentNum + random.nextInt(1000);
    }

    public static String warehBatch() {
        long time = System.currentTimeMillis() / 1000;
        if (currentTime != time) {
            currentTime = time;
            currentNum = 0;
        } else {
            currentNum += 1;
        }
        return "8001-" + time + currentNum + random.nextInt(1000);
    }

    public static String snowflakeId() {
        return String.valueOf(worker.nextId());
    }


}
